//
//  BLPageListTableViewController.m
//  BasketBallLeagues
//
//  Created by kenshin on 4/25/17.
//  Copyright © 2017 kenshin. All rights reserved.
//

#import "BLPageListTableViewController.h"
#import "BLEmptyView.h"

@interface BLPageListTableViewController ()

@property (nonatomic, assign) UITableViewStyle style;
@property (nonatomic, strong) KSTableView *listView;


@end

@implementation BLPageListTableViewController

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    if (self = [super init]) {
        _style = style;
    }
    return self;
}

- (instancetype)init
{
    return [self initWithStyle:UITableViewStylePlain];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.emptyView = (BLEmptyView *)[[[NSBundle mainBundle] loadNibNamed:@"BLEmptyView" owner:self options:nil] firstObject];
    
    [self.emptyView config:[UIImage imageNamed:@"jiahaoyou"] title:@"呃,还没有数据呢~" buttonTitle:nil];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [BLColorConfig backgroundColor];
    
    self.listView = [[KSTableView alloc] initWithFrame:self.view.bounds style:self.style];
    self.listView.backgroundColor = self.view.backgroundColor;
    self.listView.pagingListDelegate = self;
    self.listView.showsVerticalScrollIndicator = NO;
    self.listView.showsHorizontalScrollIndicator = NO;
    self.listView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.listView.tableFooterView = [UIView new];
    
    [self.view addSubview:self.listView];
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self.listView bl_beginHeaderRefreshing];
    
}

- (void)reigsterHandleEmptyView {
    self.listView.emptyDataSetSource = self;
    self.listView.emptyDataSetDelegate = self;
}

- (void)refreshData {
    [self.listView bl_beginHeaderRefreshing];
}

#pragma mark -
#pragma mark - JCPagingListViewDelegate

- (void)setHeaderRefreshingBlock:(void(^)())refreshingBlock
{
    [self.listView bl_setHeaderWithRefreshingBlock:^{
        if (refreshingBlock) {
            refreshingBlock();
        }
    }];
}

- (void)setFooterRefreshingBlock:(void(^)())refreshingBlock
{
    [self.listView bl_setFooterWithRefreshingBlock:^{
        if (refreshingBlock) {
            refreshingBlock();
        }
    }];
}

- (void)endRefreshing
{
    [self.listView bl_endHeaderRefreshing];
    [self.listView bl_endFooterRefreshing];
    
    [self reigsterHandleEmptyView];
}

- (void)endRefreshingWithNoMoreData
{
    [self.listView bl_endFooterRefreshingWithNoMoreData];
}

- (void)showErrorView:(NSError *)error setRetryBlock:(void (^)())retryBlock
{
    
}

- (void)hideErrorView
{
    
}

- (void)showErrorToast:(NSError *)error
{
    [self ks_showToast:error.localizedDescription];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - DZNEmptyDataSetSource

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:@"jiahaoyou"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"没有数据";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],
                                 NSForegroundColorAttributeName: [UIColor jc_colorWithHexString:@"#b2b2b2"]};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    NSString *text = @"点击重试";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:12.0f],
                                 NSForegroundColorAttributeName: [BLColorConfig mainColor]};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

//- (UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
//{
//    return [UIImage imageNamed:@""];
//}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return 0.0;
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
{
    return 12.0;
}

#pragma mark - DZNEmptyDataSetDelegate

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
{
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
{
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

//- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView
//{
//    return self.isLoading;
//}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
{
    [self.listView bl_beginHeaderRefreshing];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
//    [self.listView bl_beginHeaderRefreshing];
}


- (nullable UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    return self.emptyView;
}


@end
